<?php

namespace app\home\controller;

use app\common\model\cms\ClassModel;
use app\common\model\cms\DetailModel;

class CmsController extends BaseController
{
  private $classModel;

  public function __construct()
  {
    parent::__construct();
    $this->classModel = new ClassModel();
  }

  /**
   * 静态页面
   */
  public function html()
  {
    $tpl = $this->request->param('class_id', '80');
    if (is_numeric($tpl)) {
      $currentInfo = $this->cmsClass[$tpl];
      $topId = empty($currentInfo['children']) ? $currentInfo['pid'] : $tpl;
      $menuInfo = $this->cmsClass[$topId];
      $this->assign('currentInfo', $currentInfo);
      $this->assign('menuInfo', $menuInfo);
    }
    return $this->fetch($tpl);
  }

  /**
   * 列表页
   * @return mixed
   */
  public function items()
  {
    $classId = $this->request->param('class_id/d');
    $currentInfo = $this->classModel->getInfo($classId);
    if($currentInfo['class_type']==2){
      $this->redirect($currentInfo['class_url']);
    }
    if ($this->request->isAjax()) {

    }
    $topClass = $this->cmsClass[$currentInfo['location'][0]['id']];
    $this->assign('top_class', $topClass);
    $this->assign('currentInfo', $currentInfo);
    return $this->fetch($currentInfo['class_tpl']);
  }

  /**
   * 内容页
   * @return mixed
   */
  public function info()
  {
    $Id = $this->request->param('id/d');
    $classId = $this->request->param('class_id/d');
    $currentInfo = $this->classModel->getInfo($classId);
    $tableName  = $currentInfo['table_name'];
    $detailModel = new DetailModel();
    $info = $detailModel->setSuffix($tableName)->where('id',$Id)->find();
    if($tableName=='case' && !empty($info['product_ids'])){
      $info['product_list'] = $detailModel->setSuffix('product')->where('id','in',$info['product_ids'])->select();
    }
    $topClass = $this->cmsClass[$currentInfo['location'][0]['id']];
    $this->assign('top_class', $topClass);
    $this->assign('currentInfo', $currentInfo);
    $this->assign('info', $info);
    return $this->fetch($currentInfo['content_tpl']);
  }

  public function getPagelist(){
    $map        = [];
    $classModel = new ClassModel();
    $detailModel = new DetailModel();
    $classList  = $classModel->getList();
    $classId    = $this->request->param('class_id/d');
    $tableName  = 'article';
    if ($classId > 0) {
      $classInfo = $classList[$classId];
      $tableName = $classInfo['table_name'];
      $map[]     = ['class_id', 'in', (string)$classInfo['path']];
    }
    $this->request->filter(['strip_tags']);
    $pageSize   = $this->request->param('pageSize/d', 20); //每页多少条
    $pageNumber = $this->request->param('pageNumber/d'); //当前页
    $sortName   = $this->request->param('sortName'); //排序字段
    $sortOrder  = $this->request->param('sortOrder', 'desc'); //升序降序
    $order      = !empty($sortName) ? "{$sortName} {$sortOrder}" : ''; //排序
    $title      = $this->request->param('title');
    if (!empty($title)) $map[] = ['title', 'like', '%' . $title . '%'];
    try {
      $data          = ['total' => 0, 'rows' => []];
      $data['total'] = $detailModel->setSuffix($tableName)->where($map)->count();
      if ($data['total'] > 0) {
        $list         = $detailModel->setSuffix($tableName)
            ->where($map)
            ->order($order)
            ->page((int)$pageNumber, (int)$pageSize)
            ->select();
        $data['rows'] = $list;
      }
      $data['sql'] = $detailModel->setSuffix($tableName)->getLastsql();
    } catch (\Exception $e) {
      $data = ['total' => 0, 'rows' => [], 'err_msg' => $e->getMessage()];
    }
    return $data;
  }

  public function gettablename($table)
  {
    $table = \think\helper\Str::studly($table);
    $tableName = 'cms.' . $table;
    return $tableName;
  }
}